import key from 'keymaster';
import signalrService from "@/services/signalr.service";

export default {
    namespace: 'person',
    state: {
        name: '名称',
        age: 20,
        signalrMsg: ["默认消息"]
    },
    reducers: {
        edit(state: any, action: any) {
            state.name = action.payload.name;
            state.age = action.payload.age
        },
        addMsg(state: any, action: any) {
            state.signalrMsg.push(action.payload);
        }
    },
    subscriptions: {
        my({ dispatch, history }: any, onError: any) {
            key('⌘+up, ctrl+up', () => {
                dispatch({
                    type: "edit", payload: {
                        name: null,
                        age: null,
                    }
                });
            });
            signalrService.msgReceived$.subscribe(({ user, message }) => {
                const msg = `${user}说：${message}`;
                dispatch({ type: "addMsg", payload: msg });
            });
        },
    }
}
